home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter5 / write.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  9KB  |  261 lines

  1.  
  2. #define CINTERFACE
  3.  
  4. #include <windows.h>  
  5. #include "write.h"  
  6. #include "commctrl.h"
  7.  
  8.  
  9. #if defined (WIN32)
  10.     #define IS_WIN32 TRUE
  11. #else
  12.     #define IS_WIN32 FALSE
  13. #endif
  14.  
  15. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  16. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  17. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  18.  
  19. HINSTANCE hInst;   // current instance
  20.  
  21. LPCTSTR lpszAppName = "MyApp";
  22. LPCTSTR lpszTitle   = "ImageList_Write()"; 
  23.  
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26.  
  27.  
  28. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29.                       LPTSTR lpCmdLine, int nCmdShow)
  30. {
  31.    MSG      msg;
  32.    HWND     hWnd; 
  33.    WNDCLASS wc;
  34.  
  35.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  36.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  37.    wc.cbClsExtra    = 0;                      
  38.    wc.cbWndExtra    = 0;                      
  39.    wc.hInstance     = hInstance;              
  40.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  41.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  42.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  43.    wc.lpszMenuName  = lpszAppName;              
  44.    wc.lpszClassName = lpszAppName;              
  45.  
  46.    if ( IS_WIN95 )
  47.    {
  48.       if ( !RegisterWin95( &wc ) )
  49.          return( FALSE );
  50.    }
  51.    else if ( !RegisterClass( &wc ) )
  52.       return( FALSE );
  53.  
  54.    hInst = hInstance; 
  55.  
  56.    hWnd = CreateWindow( lpszAppName, 
  57.                         lpszTitle,    
  58.                         WS_OVERLAPPEDWINDOW, 
  59.                         CW_USEDEFAULT, 0, 
  60.                         CW_USEDEFAULT, 0,  
  61.                         NULL,              
  62.                         NULL,              
  63.                         hInstance,         
  64.                         NULL               
  65.                       );
  66.  
  67.    if ( !hWnd ) 
  68.       return( FALSE );
  69.  
  70.    ShowWindow( hWnd, nCmdShow ); 
  71.    UpdateWindow( hWnd );         
  72.  
  73.    while( GetMessage( &msg, NULL, 0, 0) )   
  74.    {
  75.       TranslateMessage( &msg ); 
  76.       DispatchMessage( &msg );  
  77.    }
  78.  
  79.    return( msg.wParam ); 
  80. }
  81.  
  82.  
  83. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  84. {
  85.    WNDCLASSEX wcex;
  86.  
  87.    wcex.style         = lpwc->style;
  88.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  89.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  90.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  91.    wcex.hInstance     = lpwc->hInstance;
  92.    wcex.hIcon         = lpwc->hIcon;
  93.    wcex.hCursor       = lpwc->hCursor;
  94.    wcex.hbrBackground = lpwc->hbrBackground;
  95.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  96.    wcex.lpszClassName = lpwc->lpszClassName;
  97.  
  98.    // Added elements for Windows 95.
  99.    //...............................
  100.    wcex.cbSize = sizeof(WNDCLASSEX);
  101.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  102.                             IMAGE_ICON, 16, 16,
  103.                             LR_DEFAULTCOLOR );
  104.             
  105.    return RegisterClassEx( &wcex );
  106. }
  107.  
  108.  
  109. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  110. {
  111. static HIMAGELIST hList  = NULL;
  112.  
  113.    switch( uMsg )
  114.    {
  115.       case WM_CREATE :
  116.               InitCommonControls();
  117.  
  118.               // Create the image list.
  119.               //.......................
  120.               hList = ImageList_LoadBitmap( hInst, "BMP", 32, 1, RGB( 255, 0, 0 ) );
  121.               break;
  122.  
  123.       case WM_PAINT :
  124.               {
  125.                  PAINTSTRUCT ps;
  126.  
  127.                  // Paint the images on the client area.
  128.                  //.....................................
  129.                  BeginPaint( hWnd, &ps );
  130.                  if ( hList )
  131.                  {
  132.                     ImageList_Draw( hList, 0, ps.hdc, 10, 10, ILD_NORMAL );
  133.                     ImageList_Draw( hList, 1, ps.hdc, 60, 10, ILD_NORMAL );
  134.                  }
  135.                  EndPaint( hWnd, &ps );
  136.               }
  137.               break;
  138.  
  139.       case WM_COMMAND :
  140.               switch( LOWORD( wParam ) )
  141.               {
  142.                  case IDM_SAVE :
  143.                         if ( hList )
  144.                         {
  145.                            LPSTORAGE lpst;
  146.                            LPSTREAM  lpstr;
  147.  
  148.                            // Create the Compound document.
  149.                            //..............................
  150.                            if ( StgCreateDocfile( OLESTR("Image List"), 
  151.                                                   STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE, 
  152.                                                   0, &lpst ) == S_OK )
  153.                            {
  154.                               // Create a stream within the document.
  155.                               //.....................................
  156.                               lpst->lpVtbl->CreateStream( lpst, OLESTR("Images"), 
  157.                                                   STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,
  158.                                                         0, 0, &lpstr );
  159.  
  160.                               // Save the image list in the document.
  161.                               //.....................................
  162.                               ImageList_Write( hList, lpstr );
  163.                               ImageList_Destroy( hList );
  164.                               hList = NULL;
  165.  
  166.                               InvalidateRect( hWnd, NULL, TRUE );
  167.                               UpdateWindow( hWnd );
  168.  
  169.                               // Release the IStream and IStorage objects.
  170.                               //..........................................
  171.                               lpstr->lpVtbl->Release(lpstr);
  172.                               lpst->lpVtbl->Release(lpst);
  173.  
  174.                               MessageBox( hWnd, "The image list is saved.", 
  175.                                           lpszTitle, MB_OK | MB_ICONINFORMATION );
  176.                            }
  177.                         }
  178.                         break;
  179.  
  180.                  case IDM_OPEN :
  181.                         if ( !hList )
  182.                         {
  183.                            LPSTORAGE lpst;
  184.                            LPSTREAM  lpstr;
  185.  
  186.                            // Open the compound document.
  187.                            //............................
  188.                            if ( StgOpenStorage( OLESTR("Image List"), NULL,
  189.                                                 STGM_READ | STGM_SHARE_EXCLUSIVE, 
  190.                                                 NULL, 0, &lpst ) == S_OK )
  191.                            {
  192.                               // Open the stream within the document.
  193.                               //.....................................
  194.                               lpst->lpVtbl->OpenStream( lpst, OLESTR("Images"), NULL, 
  195.                                                   STGM_READ | STGM_SHARE_EXCLUSIVE,
  196.                                                         0, &lpstr );
  197.  
  198.                               // Read the image list from the document.
  199.                               //.......................................
  200.                               hList = ImageList_Read( lpstr );
  201.                               InvalidateRect( hWnd, NULL, TRUE );
  202.  
  203.                               // Release the IStream and IStorage objects.
  204.                               //..........................................
  205.                               lpstr->lpVtbl->Release(lpstr);
  206.                               lpst->lpVtbl->Release(lpst);
  207.                            }
  208.                         }
  209.                         break;
  210.                         
  211.  
  212.                  case IDM_ABOUT :
  213.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  214.                         break;
  215.  
  216.                  case IDM_EXIT :
  217.                         DestroyWindow( hWnd );
  218.                         break;
  219.               }
  220.               break;
  221.       
  222.       case WM_DESTROY :
  223.               if ( hList )
  224.                  ImageList_Destroy( hList );
  225.  
  226.               PostQuitMessage(0);
  227.               break;
  228.  
  229.       default :
  230.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  231.    }
  232.  
  233.    return( 0L );
  234. }
  235.  
  236.  
  237.  
  238.  
  239. LRESULT CALLBACK About( HWND hDlg,           
  240.                         UINT message,        
  241.                         WPARAM wParam,       
  242.                         LPARAM lParam)
  243. {
  244.    switch (message) 
  245.    {
  246.        case WM_INITDIALOG: 
  247.                return (TRUE);
  248.  
  249.        case WM_COMMAND:                              
  250.                if (   LOWORD(wParam) == IDOK         
  251.                    || LOWORD(wParam) == IDCANCEL)    
  252.                {
  253.                        EndDialog(hDlg, TRUE);        
  254.                        return (TRUE);
  255.                }
  256.                break;
  257.    }
  258.  
  259.    return (FALSE); 
  260. }
  261.